#include<bits/stdc++.h>
#define int long long 
using namespace std;
const int N = 5e5+5,V = 5e5;
int n,cnt;
vector<int> vec1[N],vec2[N],g[N];
map<pair<int,int>,int> mp,vis;
int sz[N],ans;
void dfs(int u,int fa)
{
	for(auto v:g[u])
	{
		if(v==fa) continue;
		dfs(v,u);
		sz[u]+=sz[v];
	}
	ans+=sz[u]*(n-sz[u]);
}
signed main()
{
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i = 1,x,y;i<=n;i++)
		cin>>x>>y,vec1[x].push_back(y),vec2[y].push_back(x);
	int e = 0;
	for(int i = 0;i<=V;i++)
	{
		sort(vec1[i].begin(),vec1[i].end());
		for(auto j:vec1[i])
		{
			if(mp[{i,j-1}]) mp[{i,j}] = mp[{i,j-1}];
			else mp[{i,j}] = ++cnt;
			sz[mp[{i,j}]]++;
			if(mp[{i-1,j}])
			{
				int u = mp[{i,j}],v = mp[{i-1,j}];
				if(!vis[{u,v}])
				{
					vis[{u,v}] = vis[{v,u}] = 1,g[u].push_back(v),g[v].push_back(u);
					e++;
				}
			}
		}
	}
	dfs(1,0);
	mp.clear(),vis.clear();
	for(int i = 1;i<=cnt;i++) sz[i] = 0,g[i].clear();
	cnt = 0;
	for(int i = 0;i<=V;i++)
	{
		sort(vec2[i].begin(),vec2[i].end());
		for(auto j:vec2[i])
		{
			if(mp[{i,j-1}]) mp[{i,j}] = mp[{i,j-1}];
			else mp[{i,j}] = ++cnt;
			sz[mp[{i,j}]]++;
			if(mp[{i-1,j}])
			{
				int u = mp[{i,j}],v = mp[{i-1,j}];
				if(!vis[{u,v}]) vis[{u,v}] = vis[{v,u}] = 1,g[u].push_back(v),g[v].push_back(u);
			}
		}
	}
	dfs(1,0);
	cout<<ans;
	return 0;
}

